package com.chenfan.mcn.rule;

import com.chenfan.mcn.extension.rule.ComputableRule;
import com.chenfan.mcn.model.common.KVPair;

import java.math.BigDecimal;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 默认回款系数-计算规则集
 *
 * @author wulg
 * @date 2021-09-16
 **/
public class ReturnFactorComputableRule implements ComputableRule<BigDecimal,BigDecimal> {

    private static Map<BigDecimal, KVPair<BigDecimal,BigDecimal>> map = new ConcurrentHashMap<>(16);

    {
        /**
         *回款率达成70%及以上，则回款系数为100%
         * 回款率（含）65%-70%（不含），回款系数为90%
         * 回款率（含）60%-65%（不含），回款系数为80%
         * 回款率（含）55%-60%（不含），回款系数为70%
         * 回款率（含）50%-55%（不含），回款系数为60%
         * 按5%的档位回款系数减10%以此类推，回款系数最低为0%
         */
        //初始化规则参数
        BigDecimal percent90 = new BigDecimal("0.90");
        BigDecimal percent80 = new BigDecimal("0.80");
        BigDecimal percent70 = new BigDecimal("0.70");
        BigDecimal percent65 = new BigDecimal("0.65");
        BigDecimal percent60 = new BigDecimal("0.60");
        BigDecimal percent55 = new BigDecimal("0.55");
        BigDecimal percent50 = new BigDecimal("0.50");
        BigDecimal percent45 = new BigDecimal("0.45");
        BigDecimal percent40 = new BigDecimal("0.40");
        BigDecimal percent35 = new BigDecimal("0.35");
        BigDecimal percent30 = new BigDecimal("0.30");
        BigDecimal percent25 = new BigDecimal("0.25");
        BigDecimal percent20 = new BigDecimal("0.20");
        BigDecimal percent10 = new BigDecimal("0.10");
        map.put(BigDecimal.ONE, new KVPair<>(percent70, null));
        map.put(percent90, new KVPair<>(percent65, percent70));
        map.put(percent80, new KVPair<>(percent60, percent65));
        map.put(percent70, new KVPair<>(percent55, percent60));
        map.put(percent60, new KVPair<>(percent50, percent55));
        map.put(percent50, new KVPair<>(percent45, percent50));
        map.put(percent40, new KVPair<>(percent40, percent45));
        map.put(percent30, new KVPair<>(percent35, percent40));
        map.put(percent20, new KVPair<>(percent30, percent35));
        map.put(percent10, new KVPair<>(percent25, percent30));
        map.put(BigDecimal.ZERO, new KVPair<>(null, percent25));
    }

    @Override
    public BigDecimal execute(BigDecimal returnRate) {
        BigDecimal returnFactor = null;
        for(Map.Entry<BigDecimal,KVPair<BigDecimal,BigDecimal>> entry : map.entrySet()) {
            KVPair<BigDecimal,BigDecimal> range = entry.getValue();
            if(this.ge(returnRate, range.getK()) && this.lt(returnRate, range.getV())) {
                    returnFactor = entry.getKey();
                    break;
            }
        }
        return returnFactor;
    }

    /**
     * 大于等于
     * @param left
     * @param right
     * @return
     */
    private boolean ge(BigDecimal left, BigDecimal right) {
        return right == null || left.compareTo(right) >= 0;
    }

    /**
     * 小于
     * @param left
     * @param right
     * @return
     */
    private boolean lt(BigDecimal left, BigDecimal right) {
        return right == null || left.compareTo(right) == -1;
    }
}
